package com.chookss.home.logContacts;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.entity.LogDateEntity;
import com.chookss.home.entity.LogUserEntity;
import com.chookss.mine.personal.PersonalHomeActivity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.BasisTimesUtils;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.Nullable;

//员工每个工作日的完成情况
public class LogDateCompleteActivity extends BaseAct {

    @BindView(R.id.common_title_txt)
    TextView commonTitleTxt;
    @BindView(R.id.common_right_iv)
    ImageView commonRightIv;
    @BindView(R.id.common_right_iv_ll)
    LinearLayout commonRightIvLl;
    @BindView(R.id.tvComplete)
    TextView tvComplete;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private MyDateAdapter adapter;
    private List<LogDateEntity> list = new ArrayList<>();
    private String teamCode = "", employeeCode = "";//团队code
    private String reportType = "3";
    private String currentTime = "";//选择时间
    private String currentOldTime = "";//当前时间
    private String reportName = "日志";
    private String dateInit = "本日";
    private int nums = 7;//每一行数量
    private int itemDecoration = 28;//每一行数量
    private GridSpacingItemDecoration gridSpacingItemDecoration;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_log_date_complete);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        teamCode = getIntent().getStringExtra("teamCode");
        employeeCode = getIntent().getStringExtra("employeeCode");
        reportType = getIntent().getStringExtra("reportType");
        if ("0".equals(reportType)) {
            reportName = "日报";
            nums = 7;
            itemDecoration=(Utils.getScreenWidthPixels(this)-Utils.dip2px(this,273))/6;
        } else if ("1".equals(reportType)) {
            reportName = "周报";
            nums = 5;
            itemDecoration=(Utils.getScreenWidthPixels(this)-Utils.dip2px(this,258))/4;
        } else if ("2".equals(reportType)) {
            reportName = "月报";
            nums = 6;
            itemDecoration=(Utils.getScreenWidthPixels(this)-Utils.dip2px(this,304))/5;
        }else if ("3".equals(reportType)) {
            reportName = "日志";
            nums = 7;
            itemDecoration=(Utils.getScreenWidthPixels(this)-Utils.dip2px(this,273))/6;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        currentOldTime = simpleDateFormat.format(date);
        if (reportType.equals("2")) {
            currentOldTime = currentOldTime.substring(0, 4);
        }
        currentTime = currentOldTime;
        commonTitleTxt.setText(Utils.changeCnDate(currentTime));
        commonRightIvLl.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) commonRightIv.getLayoutParams();
        layoutParams.width = Utils.dip2px(this, 20);
        layoutParams.height = Utils.dip2px(this, 20);
        commonRightIv.setLayoutParams(layoutParams);
        commonRightIv.setBackgroundResource(R.drawable.icon_date_45);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, nums);
        recyclerView.setLayoutManager(gridLayoutManager);
        gridSpacingItemDecoration = new GridSpacingItemDecoration(nums, itemDecoration,0, false);
        recyclerView.addItemDecoration(gridSpacingItemDecoration);
        adapter = new MyDateAdapter(R.layout.item_log_date_complete, this, list);
        recyclerView.setAdapter(adapter);
        initDate();
        getData();
    }

    private void initDate() {
        if (tvComplete==null) {
            return;
        }
        if ("0".equals(reportType)) {
            if (currentTime.equals(currentOldTime)) {
                dateInit = "本月";
            } else {
                dateInit = Utils.changeCnDate(currentTime);
            }
        } else if ("1".equals(reportType)) {
            if (currentTime.equals(currentOldTime)) {
                dateInit = "本月";
            } else {
                dateInit = Utils.changeCnDate(currentTime);
            }
        } else if ("2".equals(reportType)) {
            if (currentTime.equals(currentOldTime)) {
                dateInit = "本年";
            } else {
                dateInit = Utils.changeCnDate(currentTime);
            }
        } else if ("3".equals(reportType)) {
            if (currentTime.equals(currentOldTime)) {
                dateInit = "本月";
            } else {
                dateInit = Utils.changeCnDate(currentTime);
            }
        }
        tvComplete.setText(dateInit + reportName + "提交情况");
    }

    private void getData() {
        showLoading();
        Map<String, String> map = new HashMap<>();
        map.put("periodName", currentTime);
        map.put("teamCode", teamCode);
        map.put("reportType", reportType);
        map.put("employeeCode", employeeCode);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<LogDateEntity>>>() {
        }.getType(), null, Urls.selectReportEmpDone, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<LogDateEntity>>() {
            @Override
            public void onSuccess(List<LogDateEntity> data) {
                dismissLoading();
                list.clear();
                list.addAll(data);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String errorMsg) {
                dismissLoading();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });


    }

    int mYear = 0, mMonth = 0, mDay = 0;
    String monthS = "", dayS = "";

    private void showDatePickDialog(int state) {
        //获取日历的一个实例，里面包含了当前的年月日
        Calendar calendar = Calendar.getInstance();
        if (mYear == 0) {
            mYear = calendar.get(Calendar.YEAR);
            mMonth = calendar.get(Calendar.MONTH);
            mDay = calendar.get(Calendar.DAY_OF_MONTH);
        }
        BasisTimesUtils.showDatePickerDialog(this, true, "请选择日期", mYear, mMonth, mDay,
                new BasisTimesUtils.OnDatePickerListener() {

                    @Override
                    public void onConfirm(int year, int month, int dayOfMonth) {
                        mYear = year;
                        mMonth = month - 1;
                        mDay = dayOfMonth;
                        if (commonTitleTxt==null) {
                            return;
                        }
                        if (dayOfMonth > 9) {
                            dayS = dayOfMonth + "";
                        } else {
                            dayS = "0" + dayOfMonth;
                        }
                        if (month > 9) {
                            monthS = month + "";
                        } else {
                            monthS = "0" + month;
                        }
                        if ("2".equals(reportType)) {
                            commonTitleTxt.setText(year + "年");
                            currentTime = year + "";
                        } else {
                            commonTitleTxt.setText(year + "年" + monthS + "月");
                            currentTime = year + "-" + monthS;
                        }
                        initDate();
                        getData();
                    }

                    @Override
                    public void onCancel() {
                    }
                }).setDay(state);
    }

    @OnClick({R.id.common_title_back, R.id.common_right_iv_ll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_iv_ll:
                if ("2".equals(reportType)) {
                    showDatePickDialog(3);
                } else {
                    showDatePickDialog(2);
                }
                break;
        }
    }

    class MyDateAdapter extends BaseQuickAdapter<LogDateEntity, BaseViewHolder> {
        private Context context;

        public MyDateAdapter(int layoutResId, Context context, @Nullable List<LogDateEntity> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, LogDateEntity item) {
            TextView tv45 = helper.getView(R.id.tv45);
            TextView tv35 = helper.getView(R.id.tv35);
            if ("0".equals(reportType) || "3".equals(reportType)) {
                tv35.setVisibility(View.VISIBLE);
                tv45.setVisibility(View.GONE);
                tv35.setText(item.getDay());
                if ("1".equals(item.getDone())) {
                    tv35.setTextColor(getResources().getColor(R.color.text_44d3b3));
                    tv35.setBackgroundResource(R.drawable.shape_circle_daf6f0);
                } else {
                    tv35.setTextColor(getResources().getColor(R.color.text_gray3));
                    tv35.setBackgroundResource(R.drawable.shape_circle_white);
                }
            } else {
                tv35.setVisibility(View.GONE);
                tv45.setVisibility(View.VISIBLE);
                if ("1".equals(reportType)) {
                    tv45.setText("第" + item.getDay() + "周");
                    tv45.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
                } else if ("2".equals(reportType)) {
                    tv45.setText(item.getDay() + "月");
                    tv45.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
                }
                if ("1".equals(item.getDone())) {
                    tv45.setTextColor(getResources().getColor(R.color.text_44d3b3));
                    tv45.setBackgroundResource(R.drawable.shape_circle_daf6f0);
                } else {
                    tv45.setTextColor(getResources().getColor(R.color.text_gray3));
                    tv45.setBackgroundResource(R.drawable.shape_circle_white);
                }
            }
        }

    }

}
